﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmMain : Form
    {

       




        public frmMain()
        {
            InitializeComponent();
        }

#region 폼 소스
        private void frmMain_Load(object sender, EventArgs e)
        {
            // TODO: 이 코드는 데이터를 'dataSet1.fieldinfoIsCode' 테이블에 로드합니다.
            this.fieldinfoIsCodeTableAdapter.Fill(this.dataSet1.fieldinfoIsCode);
            // TODO: 이 코드는 데이터를 'dataSet1.fieldinfoIsRequied' 테이블에 로드합니다.
            this.fieldinfoIsRequiedTableAdapter.Fill(this.dataSet1.fieldinfoIsRequied);
            // TODO: 이 코드는 데이터를 'dataSet11.fieldinfoType' 테이블에 로드합니다.
            this.fieldinfoTypeTableAdapter.Fill(this.dataSet1.fieldinfoType);


            // TODO: 이 코드는 데이터를 'dataSet1.LAYERINFO' 테이블에 로드합니다.
            this.layerInfoTableAdapter.Fill(this.dataSet1.LAYERINFO);
            // TODO: 이 코드는 데이터를 'dataSet1.FIELDINFO' 테이블에 로드합니다.
            this.fieldInfoTableAdapter.FillByCode(this.dataSet1.FIELDINFO, "");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

#endregion

#region 테이블의 첫 컬럼에 숫자 표시하기
        // 레이어정보 테이블 첫 컬럼에 숫자표시
        private void dgvLayerInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rect = new Rectangle(e.RowBounds.Location.X,

                         e.RowBounds.Location.Y,

                         dgvLayerInfo.RowHeadersWidth - 4,

                         e.RowBounds.Height);
         // 위에서 생성된 장방형내에 행번호를 보여주고 폰트색상 및 배경을 설정
        TextRenderer.DrawText(e.Graphics,

                         (e.RowIndex + 1).ToString(), 

                         dgvLayerInfo.RowHeadersDefaultCellStyle.Font,

                         rect,  

                         dgvLayerInfo.RowHeadersDefaultCellStyle.ForeColor,

                         TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

        }
        // 필드정보 테이블 첫 컬럼에 숫자표시
        private void dgvFieldInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rect = new Rectangle(e.RowBounds.Location.X,
                         e.RowBounds.Location.Y,
                         dgvFieldInfo.RowHeadersWidth - 4,
                         e.RowBounds.Height);
            // 위에서 생성된 장방형내에 행번호를 보여주고 폰트색상 및 배경을 설정
            TextRenderer.DrawText(e.Graphics,
                             (e.RowIndex + 1).ToString(),
                             dgvFieldInfo.RowHeadersDefaultCellStyle.Font,
                             rect,
                             dgvFieldInfo.RowHeadersDefaultCellStyle.ForeColor,
                             TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
#endregion


        private string selectedLayerinfoCode = "";
        private void dgvLayerInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectDgvLayerInfo(e);            
        }

        private void selectDgvLayerInfo(DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                selectedLayerinfoCode = dgvLayerInfo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                this.fieldInfoTableAdapter.FillByCode(this.dataSet1.FIELDINFO, selectedLayerinfoCode);
            }
        }

        private string selectedFieldinfoCode = "";
        private void dgvFieldInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            updateDgvFieldInfo(e);            
        }



        private void updateDgvFieldInfo(DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                selectedFieldinfoCode = dgvFieldInfo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                string fieldnm = dgvFieldInfo.Rows[e.RowIndex].Cells[1].FormattedValue.ToString();
                string type = dgvFieldInfo.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
                string isrequied = dgvFieldInfo.Rows[e.RowIndex].Cells[3].FormattedValue.ToString();
                string iscode = dgvFieldInfo.Rows[e.RowIndex].Cells[4].FormattedValue.ToString();
                string min = dgvFieldInfo.Rows[e.RowIndex].Cells[5].FormattedValue.ToString();
                string max = dgvFieldInfo.Rows[e.RowIndex].Cells[6].FormattedValue.ToString();

                frmFieldInfo frmFieldInfo1 = new frmFieldInfo();

                frmFieldInfo1.txtCode.Text = selectedFieldinfoCode;
                frmFieldInfo1.txtFieldnm.Text = fieldnm;
                frmFieldInfo1.cmbType.Text = type;
                frmFieldInfo1.cmbIsrequied.Text = isrequied;
                frmFieldInfo1.cmbIscode.Text = iscode;
                frmFieldInfo1.txtMin.Text = min;
                frmFieldInfo1.txtMax.Text = max;

                if (frmFieldInfo1.ShowDialog() == DialogResult.OK)
                {
                    fieldInfoTableAdapter.UpdateFieldinfoQuery(frmFieldInfo1.cmbType.Text,
                        frmFieldInfo1.cmbIsrequied.Text, frmFieldInfo1.cmbIscode.Text,
                        int.Parse(frmFieldInfo1.txtMin.Text), int.Parse(frmFieldInfo1.txtMax.Text), frmFieldInfo1.txtFieldnm.Text);
                    
                    //dgvFieldInfo.Refresh();
                    this.fieldInfoTableAdapter.FillByCode(this.dataSet1.FIELDINFO, selectedLayerinfoCode);

                }

            }
        }

        private void btnEditCodeInfo_Click(object sender, EventArgs e)
        {

        }
        

        
    }// class
}// namespace
